// pages/order/order.js
import {orderStatus} from "../../utils/data";
import {comment, confirmReceipt, getOrderList} from "../../service/order";
import {deleteAddress} from "../../service/address";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderStatus,
    orders:[],
    status:1,
    page:1,
    isLoading:false,
    isLast:false,
    show:false,
    currentCommentOrder:{},
    radio:'',
    value:'',
    content:'',
    rate:'',
    star:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  /**
   * 获取数据的方法
   */
  getData(){
    this.setData({isLoading:true})

    const {page,status} = this.data
    //准备查询参数
    const data = {
      page,
      status,
      include: 'orderDetails.goods'
    }
    getOrderList(data).then(res=>{
      this.setData({isLoading: false})

      // console.log(res)
      this.setData({
        orders: [...this.data.orders, ...res.data] //将新的数据，追加到旧的数据列表里面
      })
      // console.log(this.data.orders)
      //判断是否是最后一页
      if(this.data.page == res.meta.pagination.total_pages){
        this.setData({
          isLast: true
        })
      }
    })
  },
  /**
   * tab标签点击事件
   */
  onClick(event){
    const {index} = event.detail

    this.setData({status:index + 1})

    //标签页切换的时候，重置数据
    this.setData({
      isLast: false,
      page:1,
      orders:[]
    })
    this.getData()
  },

  /**
   * 跳转到订单详情
   */
  toOrderInfo(event){
    // console.log(event)
    const id = event.currentTarget.id
    wx.navigateTo({
      url:'/pages/order_info/order_info?order_id=' + id,
    })
  },
  /**
   * 确认收货
   * @param event
   */
  confirm_receipt(event){
    //1.弹窗确认是否 确认收货
    wx.showModal({
      title:'提示',
      content:'确定是否收货',
      success:(res)=> {
        if(!res.confirm) return
        //2.获取到订单ID
        const id= event.currentTarget.id
        //请求API，确认收货
        confirmReceipt(id).then(()=>{
          //4.弹窗提醒确认成功
          wx.showToast({
            title:'收货成功',
            icon:'success'
          })
          //5.将已确认收货的这条数据移除
          this.setData({
            orders: this.data.orders.filter(item=>item.id != id)
          })
        })
      }
    })
  },

  /**
   * 打开订单评价窗口
   */
  openCommentOrder(event){
    //1.获取订单ID
    const id = event.currentTarget.id

    //2.让弹出框显示
    this.setData({show: true})

    //3.获取当前操作的订单数据，后边执行评价的时候会用到
    //主要是执行评价的时候，需要用到goods_id
    // console.log(event)
    const order = event.currentTarget.dataset.order

    //4.缓存订单数据
    this.setData({
      currentCommentOrder:order
    })
    // console.log(this.data.currentCommentOrder.orderDetails)
  },
  onClose(){
    this.setData({show:false})
  },
  /**
   * rate
   * @param event
   */
  rateChange(event){
    console.log(event.detail)
    this.setData({
      rate:event.detail
    })
  },
  /**
   * star
   * @param event
   */
  starChange(event){
    console.log(event.detail)
    this.setData({
      star:event.detail
    })
  },
  /**
   * 触底加载更多
   */
  onReachBottom(){
    if(this.data.isLast) return

    this.setData({
      page:this.data.page + 1
    })
    this.getData()
  },
  /**
   * 获取文本框数据
   * @param event
   */
  textTap(event){
    console.log(event)
    console.log(event.detail.value)

    this.setData({
      content:event.detail.value
    })
    console.log(this.data.content)

  },
  /**
   * 执行订单评价
   */
  doCommentOrder(event){
    //1.准备提交的部分数据
    console.log(this.data.content)

    let data = {
      content:this.data.content,
      rate:this.data.rate,
      star:this.data.star
    }
    console.log(data)
    //2.循环订单细节，追加商品ID，发送API请求，执行评论
    this.data.currentCommentOrder.orderDetails.data.forEach(item=>{
      console.log(item)
      //追加商品ID
      data.goods_id = item.goods_id
      //发送API，执行评论
      comment(item.order_id,data).then(res=>{
        //弹窗提醒
        wx.showToast({
          title:'评论完成',
          icon:'success'
         })
        this.setData({
          show:false
        })
      })
    })
  }
})